﻿using System.Linq;
using System.Windows;

namespace DynamicGeometry
{
    public interface IMovable
    {
        void MoveTo(Point position);
        bool AllowMove();
        Point Coordinates { get; }
    }

    public static class IMovableExtensions
    {
        public static void MoveTo(this IMovable movable, Point newPosition, Drawing drawing)
        {
            MoveAction action = new MoveAction(drawing, new[] { movable }, newPosition.Minus(movable.Coordinates), null);
            drawing.ActionManager.RecordAction(action);
        }

        public static void MoveAndRecordAction(this IFigure figure, Point newPosition)
        {
            var drawing = figure.Drawing;
            var movable = figure as IMovable;
            if (movable == null)
            {
                return;
            }
            var toRecalculate = DependencyAlgorithms.FindDescendants
                (f => f.Dependents, figure).Reverse();
            var action = new MoveAction(
                drawing, 
                movable.AsEnumerable(), 
                newPosition.Minus(movable.Coordinates), 
                toRecalculate);
            drawing.ActionManager.RecordAction(action);
        }

        public static void MoveTo(this IMovable movable, double x, double y)
        {
            movable.MoveTo(new Point(x, y));
        }
    }
}
